package com.example.tacocloudmessagingrabbitmq;

import com.example.tacoclouddomain.Order;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;

@Component
public class RabbitOrderReceiver {

    private RabbitTemplate rabbit;
    private MessageConverter converter;

    @Autowired
    public RabbitOrderReceiver(RabbitTemplate rabbit, MessageConverter converter) {
        this.rabbit = rabbit;
        this.converter = converter;
    }

    public Order receiveOrder(){
        return rabbit.receiveAndConvert("tacocloud.order.queue", 3000,
                new ParameterizedTypeReference<Order>() {
                    @Override
                    public Type getType() {
                        return super.getType();
                    }
                });
    }
}
